package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.domain.DataMain;
import com.uwlaser.service.DataMainService;
import com.uwlaser.service.dto.DataMainDto;
import com.uwlaser.service.dto.DataMainQueryParam;
import com.uwlaser.service.dto.MaterialTreeVo;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "产品总览管理")
@RequestMapping("/api/dataMain")
public class DataMainController {

    private final DataMainService dataMainService;

    @GetMapping
    @Log("查询产品总览")
    @ApiOperation("查询产品总览")
    @PreAuthorize("@el.check('dataMain:list')")
    public ResponseEntity query(DataMainQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dataMainService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增产品总览")
    @ApiOperation("新增产品总览")
    @PreAuthorize("@el.check('dataMain:add')")
    public ResponseEntity create(@Validated @RequestBody DataMainDto resources){
        return new ResponseEntity<>(dataMainService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改产品总览")
    @ApiOperation("修改产品总览")
    @PreAuthorize("@el.check('dataMain:edit')")
    public ResponseEntity update(@Validated @RequestBody DataMainDto resources){
        dataMainService.updateById(resources);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除产品总览")
    @ApiOperation("删除产品总览")
    @PreAuthorize("@el.check('dataMain:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        dataMainService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出产品总览")
    @ApiOperation("导出产品总览")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dataMain:list')")
    public void download(HttpServletResponse response, DataMainQueryParam query) throws IOException {
        dataMainService.download(dataMainService.queryAll(query), response);
    }

    @PostMapping("/getById")
    @Log("查询单个产品")
    @ApiOperation("查询单个产品")
    @PreAuthorize("@el.check('dataMain:list')")
    public ResponseEntity getById(@RequestBody DataMainQueryParam query){
        return new ResponseEntity<>(dataMainService.getById(query.getId()),HttpStatus.OK);
    }

    @GetMapping("/getMaterialTree")
    @Log("根据条码获取物料树")
    @ApiOperation("根据条码获取物料树")
    @PreAuthorize("@el.check('dataMain:list')")
    public ResponseEntity getMaterialTree(MaterialTreeVo query){
        return new ResponseEntity<>(dataMainService.getMaterialTree(query),HttpStatus.OK);
    }

    @Log("修改产品型号和参数的关系")
    @ApiOperation("修改产品型号和参数的关系")
    @PutMapping(value = "/productParameter")
    public ResponseEntity<Object> updateProduct(@RequestBody DataMainDto resources){
        dataMainService.updateProduct(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
